<?php
/**
 * Created by PhpStorm.
 * User: lu
 * Date: 2018/6/25
 * Time: 17:56
 */

namespace App\Tools;

use App\Models\Order;
use App\Models\Shop;
use App\Models\ShopRoomTable;
use JPush\Client as JPushClient;

class Jpush{

    //推送给所有用户
    public function sendAll(){
        try{
            $jpush = new JPushClient('443eca432eff14581d12c724', '6e8484efcc1d8b85756ba5af');
            $response = $jpush->push()
                //推送平台
                ->setPlatform('all')
                ->addAllAudience()
                ->setNotificationAlert('呼叫榜哥，呼叫榜哥')
                ->send();
        }catch (\Exception $e){
            return;
        }
    }

    //终止语音
    public function stopcalling($order_id){
        try {
            $order = Order::find($order_id);
            if (!$order) {
                return;
            }
            //获取推送别名
            $shop = Shop::find($order->shop_id);
            if (!$shop) {
                return;
            } else {
                $alias = '0_' . $shop->user_id;
            }
            //推送空消息
            $jpush = new JPushClient('443eca432eff14581d12c724', '6e8484efcc1d8b85756ba5af');
            $data = $jpush->push()
                //推送平台
                ->setPlatform('all')
                //用户别名
                ->addAlias($alias)
                //消息和语音(安卓与ios)
                ->androidNotification()
                ->iosNotification()
                ->options(array(
                    'time_to_live' => 86400,
                    'apns_production' => true,
                ))
                ->send();
        }catch (\Exception $e){
            return;
        }
    }
    //订单语音
    public function sendOrder($order_id=false){
        try {
            $order = Order::find($order_id);
            if (!$order) {
                return;
            }
            //获取推送内容
            switch ($order->order_type) {
                case 0://外卖
                    if ($order->order_status == 2) {
                        $title = "您有一个外卖订单";
                        $sound = "您有一个外卖订单,请注意接单";
                    } else {
                        return;
                    }
                    break;
                case 1://预定
                    if ($order->order_status == 2) {
                        $title = "您有一个预定订单";
                        $sound = "您有一个预定订单,请注意查看";
                    } else if ($order->order_status == 4) {
                        $title = "您有一个预定订单已结账,收到金额" . $order->order_price . "元";
                        $sound = "您有一个预定订单已结账,收到金额" . $order->order_price . "元整,请注意查看";
                    } else {
                        return;
                    }
                    break;
                case 3://扫码
                    if ($order->order_status == 4) {
                        $title = "您有一个扫码订单（已付款）";
                        $sound = "您有一个扫码订单,请注意查看";
                    } else {
                        return;
                    }
                    break;
                default://错误
                    return;
                    break;

            }
            //获取推送别名
            $shop = Shop::find($order->shop_id);
            if (!$shop) {
                return;
            } else {
                $alias = '0_' . $shop->user_id;
            }
            //推送消息
            $this->send($alias, $title, $sound, $order_id);
        }catch (\Exception $e){
            return;
        }
    }



    //呼叫语音
    public function callSmallTwo($order_id=false){
        try {
            $order = Order::find($order_id);
            if (!$order) {
                return;
            }
            $shop = Shop::find($order->shop_id);
            if (!$shop) {
                return;
            }
            //获取推送内容
            switch ($order->order_type) {
                case 1://预定
                    if ($order->order_status == 3) {
                        //获取房间
                        $order_reserve = $order->orderReserve;
                        if (!$order_reserve) {
                            return;
                        }
                        $desk_room = explode('|', $order_reserve->desk_room);
                        if (count($desk_room) == 1 && $desk_room[0] == '') {
                            return;
                        } else {
                            //获取房桌
                            foreach ($desk_room as $k => $v) {
                                $shoproomtable = ShopRoomTable::find($v);
                                if ($shoproomtable) {//存在房桌
                                    $title = $shoproomtable->name . '呼叫服务员';
                                    $sound = $shoproomtable->name . '呼叫服务员';
                                    if ($shoproomtable->shoper_id == null) {//为空推送给商家
                                        $alias = '0_' . $shop->user_id;
                                    } else {//不为空推送给服务员们
                                        $alias = explode('|', $shoproomtable->shoper_id);
                                        //处理多个服务员
                                        foreach ($alias as $k2 => $v2) {
                                            $alias[$k] = '1_' . $v2;
                                        }
                                    }
                                    $this->send($alias, $title, $sound, $order_id);
                                }
                            }
                        }
                    } else {
                        return;
                    }
                    return;
                    break;
                case 3://扫码
                    if ($order->order_status == 3) {
                        //$order_reserve = $order->orderscan;
                        //$title = $order_reserve->desk_name."呼叫服务员";
                        //$sound = $order_reserve->desk_name."呼叫服务员";
                        return;
                    } else {
                        return;
                    }
                    break;
                default://错误
                    return;
                    break;
            }
        }catch (\Exception $e){
            return;
        }
    }

    protected function send($alias,$title,$sound,$order_id){
        try{
            $jpush = new JPushClient('443eca432eff14581d12c724', '6e8484efcc1d8b85756ba5af');
            $response = $jpush->push()
                //推送平台
                ->setPlatform('all')
                //用户别名
                ->addAlias($alias)
                //消息和语音(安卓与ios)
                ->androidNotification($title,array(
                    'extras' => array(
                        'sound' => $sound,
                        'order_id'  => $order_id
                    ),
                ))
                ->iosNotification($title,array(
                    'extras' => array(
                        'sound' => $sound,
                        'order_id'  => $order_id
                    ),
                ))
                ->options(array(
                    'time_to_live' => 86400,
                    'apns_production' => true,
                ))
                ->send();
            //dd( $response);
        }catch (\Exception $e){
            //dd($e->getMessage());
        }
    }


}